package com.markby.fscm.cryptogram.domain.standard0018;

import lombok.Getter;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;

import static com.markby.fscm.cryptogram.domain.standard0016.Constant0016.ECC_MAX_MODULUS_BITS_LEN;


/**
 * 0018-2023规范 ECC私钥数据结构
 * ULONG 为32位无符号整数
 * typedef struct ECCrefPrivateKey_st
 * {
 * 	ULONG bits;					// 密钥位长（4字节）
 * 	BYTE K[ECCref_MAX_LEN];		// 私钥（ECCref_MAX_LEN字节）
 * }ECCrefPrivateKey;
 */
public class ECCrefPrivateKey {

    private int bits;
    @Getter
    private byte[] K = new byte[Constant0018.ECCref_MAX_LEN];

    public ECCrefPrivateKey(BCECPrivateKey bcecPrivateKey, int ulBits) {
        this.bits = ulBits;
        byte[] dBytes = bcecPrivateKey.getD().toByteArray();

//        if (dBytes.length > ECC_MAX_MODULUS_BITS_LEN / 8) {
//            throw new IllegalArgumentException("Invalid PrivateKey length: " + dBytes.length);
//        }

        if (bits == 256) {
            int len = ECC_MAX_MODULUS_BITS_LEN / 8 / 2;
            System.arraycopy(dBytes, dBytes.length - len, K, K.length - len, len);
        } else if (bits == 512) {
            int len = ECC_MAX_MODULUS_BITS_LEN / 8;
            System.arraycopy(dBytes, dBytes.length - len, K, K.length - len, len);
        }
    }
}
